package com.louis.pluginproject;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import androidx.annotation.Nullable;

import com.louis.stander.ActivityInterface;

import java.lang.reflect.Constructor;


/**
 * 代理的Activity 代理/占位 插件里面的Activity
 */
public class ProxyActivity extends Activity {

    private static final String TAG = "ProxyActivity";

    @Override
    public Resources getResources() {
        return PluginManager.getInstance(this).getResources();
    }

    @Override
    public ClassLoader getClassLoader() {
        return PluginManager.getInstance(this).getDexClassLoader();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //真正的加载插件里面的Activity
        String className = null;
        className = getIntent().getStringExtra("className");
        try {
            Class mPluginActivityClass = getClassLoader().loadClass(className);
            //实例化插件包里面的Activity
            Constructor constructor = mPluginActivityClass.getConstructor(new Class[]{});
            Object mPluginActivity = constructor.newInstance(new Object[]{});
            ActivityInterface activityInterface = (ActivityInterface) mPluginActivity;

            //注入
            activityInterface.insertAppContext(this);

            Bundle bundle = new Bundle();
            bundle.putString("appName", "我是宿主传递过来的信息");
            //执行插件里面的onCreate方法
            activityInterface.onCreate(bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void startActivity(Intent intent) {

        String className = intent.getStringExtra("className");
        Log.d(TAG, "startActivity " + className);

        //要给TestActivity进栈
        Intent proxyIntent = new Intent(this, ProxyActivity.class);
        proxyIntent.putExtra("className", className);

        super.startActivity(proxyIntent);
    }

    @Override
    public ComponentName startService(Intent service) {

        String className = service.getStringExtra("className");
        Log.d(TAG, "startService " + className);

        Intent intent = new Intent(this, ProxyService.class);
        intent.putExtra("className", className);

        return super.startService(intent);
    }

    @Override
    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {

        String pluginMyReceiver = receiver.getClass().getName();

        //注册广播
        return super.registerReceiver(new ProxyReceiver(pluginMyReceiver), filter);
    }

    @Override
    public void sendBroadcast(Intent intent) {
        super.sendBroadcast(intent);
    }
}
